package com.sds.ai.impl.data.logging;


import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;
import com.google.inject.servlet.SessionScoped;
import com.sds.ai.impl.data.HasKey;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import java.io.Serializable;
import java.util.Date;

@SessionScoped
@Entity
@XmlType(propOrder = {"key", "start", "end", "messages", "value"})
public class ChatSession implements HasKey, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    private Date start;

    private Date end;

    private Text value = new Text("");

    public ChatSession() {
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    @XmlElement
    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    @XmlElement
    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    @XmlElement
    @Transient
    public String getMessages() {
        return value.getValue();
    }

    public Text getValue() {
        return value;
    }

    public void setValue(Text value) {
        this.value = value;
    }

    public void appendMessage(String message) {
        String currentValue = this.value.getValue();
        currentValue += "\n";
        currentValue += message;
        this.value = new Text(currentValue);
    }
}
